package com.zlll.winner.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * rabbitmq延时配置
 * @author Administrator
 */
@Configuration
public class RabbitDelayConfig {

    public static final String ACTIVE_DELAY_QUEUE = "activity.delayQueue";
    public static final String ACTIVE_DELAY_EXCHANGE = "activity.delayExchange";
    public static final String ACTIVE_DELAY_ROUTING_KEY = "activity_delayKey";

    /**
    * @Description: 延时队列交换机器，类型为：CustomExchange
    */
    @Bean
    public CustomExchange activeDelayExchange() {
        Map<String,Object> args = new HashMap<>(1);
        args.put("x-delayed-type","direct");
        //属性参数:交换机名称 交换机类型 是否持久化 是否自动删除 配置参数
        return new CustomExchange(ACTIVE_DELAY_EXCHANGE,"x-delayed-message",true,false,args);
    }

    /**
     * @Description: 延时队列
     */
    @Bean
    public Queue activeDelayQueue() {
        //属性参数:队列名称 是否持久化
        return new Queue(ACTIVE_DELAY_QUEUE,true);
    }


    /**
     * @Description: 给延时队列绑定交换机
     */
    @Bean
    public Binding activeDelayBinding() {
        return BindingBuilder.bind(
                activeDelayQueue())
                .to(activeDelayExchange())
                .with(ACTIVE_DELAY_ROUTING_KEY)
                .noargs();
    }


    public static final String MALL_DELAY_QUEUE = "mall.delayQueue";
    public static final String MALL_DELAY_EXCHANGE = "mall.delayExchange";
    public static final String MALL_DELAY_ROUTING_KEY = "mall_delayKey";


    /**
     * @Description: 延时队列交换机器，类型为：CustomExchange
     */
    @Bean
    public CustomExchange mallDelayExchange() {
        Map<String,Object> args = new HashMap<>(1);
        args.put("x-delayed-type","direct");
        //属性参数:交换机名称 交换机类型 是否持久化 是否自动删除 配置参数
        return new CustomExchange(MALL_DELAY_EXCHANGE,"x-delayed-message",true,false,args);
    }

    /**
     * @Description: 延时队列
     */
    @Bean
    public Queue mallDelayQueue() {
        //属性参数:队列名称 是否持久化
        return new Queue(MALL_DELAY_QUEUE,true);
    }


    /**
     * @Description: 给延时队列绑定交换机
     */
    @Bean
    public Binding mallDelayBinding() {
        return BindingBuilder.bind(
                mallDelayQueue())
                .to(mallDelayExchange())
                .with(MALL_DELAY_ROUTING_KEY)
                .noargs();
    }


    public static final String LIVE_LISTEN_LOG_QUEUE = "live.listen.log.queue";
    public static final String LIVE_LISTEN_LOG_EXCHANGE = "live.listen.log.exchange";
    public static final String LIVE_LISTEN_LOG_ROUTING_KEY = "live.listen.log.key";


    /**
     * @Description: 延时队列交换机器，类型为：CustomExchange
     */
    @Bean
    public CustomExchange liveListenLogExchange() {
        Map<String,Object> args = new HashMap<>(1);
        args.put("x-delayed-type","direct");
        //属性参数:交换机名称 交换机类型 是否持久化 是否自动删除 配置参数
        return new CustomExchange(LIVE_LISTEN_LOG_EXCHANGE,"x-delayed-message",true,false,args);
    }

    /**
     * @Description: 延时队列
     */
    @Bean
    public Queue liveListenLogQueue() {
        //属性参数:队列名称 是否持久化
        return new Queue(LIVE_LISTEN_LOG_QUEUE,true);
    }


    /**
     * @Description: 给延时队列绑定交换机
     */
    @Bean
    public Binding liveListenLogBinding() {
        return BindingBuilder.bind(liveListenLogQueue())
                .to(liveListenLogExchange())
                .with(LIVE_LISTEN_LOG_ROUTING_KEY)
                .noargs();
    }

}
